package com.yazi.mxz.admin.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.myframework.smhj.common.exception.MyException;
import com.myframework.smhj.common.tool.DateUtil;
import com.myframework.smhj.common.tool.PageLink;
import com.myframework.smhj.hibernate.Searching;
import com.myframework.smhj.hibernate.Page;
import com.myframework.smhj.hibernate.Sort;
import com.myframework.smhj.jackson.result.StatusResult;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yazi.mxz.common.controller.BaseController;
import com.yazi.mxz.common.entity.beans.Cook;
import com.yazi.mxz.common.entity.beans.Material;
import com.yazi.mxz.common.service.CookService;
import com.yazi.mxz.common.service.MaterialService;

@Controller("cookController")
@RequestMapping("/cook")
public class CookController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(CookController.class);
	
	@Resource
	CookService cookService;
	@Resource
	MaterialService materialService;
	
	@RequestMapping("/list")
	public String list(Cook cook, Searching si, Model model){
		log.debug("进入菜谱列表页面");
		// 默认排序
		if (null == si.getSorts()) {
			List<Sort> sorts = new ArrayList<Sort>();
			sorts.add(new Sort("sort", true));
			si.setSorts(sorts);
		}
		Page<Cook> page = cookService.list(cook, si);
		PageLink pageLink = new MyPageLink(si);
		model.addAttribute("cookList", page.getList());
		model.addAttribute("cook", cook);
		model.addAttribute("pi", page.getPaging());
		model.addAttribute("pageLink", pageLink);
		model.addAttribute("searchWord", si.getSearchWord());
		return "cook_list";
	}
	
	@RequestMapping("/addOrEdit")
	public String addOrEdit(Long cookId, Model model){
		Cook cook = null;
		if (null != cookId) {
			log.debug("进入菜谱编辑页面：cookId={}", cookId);
			cook = cookService.one(cookId);
			Material material = new Material();
			material.setCook(cook);
			List<Material> materialList = materialService.list(material);
			model.addAttribute("materialList", materialList);
		} else {
			log.debug("进入菜谱添加页面");
		}
		model.addAttribute("cook", cook);
		return "cook_addOrEdit";
	}
	
	@RequestMapping("/addOrEditDo/json")
	@ResponseBody
	public StatusResult addOrEditJson(Cook cook, ListObj listObj){
		log.debug("保存（添加/更新）菜谱: name={}", cook.getName());
		StatusResult jsonResult = new StatusResult();
		List<Material> materials = listObj.getMats();
		try {
			cookService.addOrEdit(cook, materials);
			jsonResult.setOk();
		} catch (MyException e) {
			log.error("保存（添加/更新）菜谱异常：", e);
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}
	
	@RequestMapping("/recommendDo/json")
	@ResponseBody
	public StatusResult recommendDoJson(Long cookId){
		log.debug("菜谱推荐: cookId={}", cookId);
		StatusResult jsonResult = new StatusResult();
		try{
			Cook cook = cookService.one(cookId);
			cook.setIsRecommend(cook.getIsRecommend()?false:true);
			cookService.merge(cook);
			jsonResult.setOk();
		} catch (Exception e) {
			log.error("菜谱推荐异常：", e);
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}
	
	@RequestMapping("/stickDo/json")
	@ResponseBody
	public StatusResult stickDoJson(Long cookId){
		log.debug("菜谱置顶: cookId={}", cookId);
		StatusResult jsonResult = new StatusResult();
		try{
			Cook cook = cookService.one(cookId);
			cook.setSort(DateUtil.getTimeStamp());
			cookService.merge(cook);
			jsonResult.setOk();
		} catch (Exception e) {
			log.error("菜谱置顶异常：", e);
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}
	
	@RequestMapping("/sinkDo/json")
	@ResponseBody
	public StatusResult sinkDoJson(Long cookId){
		log.debug("菜谱沉底: cookId={}", cookId);
		StatusResult jsonResult = new StatusResult();
		try{
			Cook cook = cookService.one(cookId);
			cook.setSort(0L);
			cookService.merge(cook);
			jsonResult.setOk();
		} catch (Exception e) {
			log.error("菜谱沉底异常：", e);
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}
	
	@RequestMapping("/delDo/json")
	@ResponseBody
	public StatusResult delDoJson(Long cookId){
		log.debug("删除菜谱: cookId={}", cookId);
		StatusResult jsonResult = new StatusResult();
		try{
			cookService.remove(cookId);
			jsonResult.setOk();
		} catch (Exception e) {
			log.error("删除菜谱异常：", e);
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}
	
	
	public static class ListObj {
		List<Material> mats;

		public List<Material> getMats() {
			return mats;
		}

		public void setMats(List<Material> mats) {
			this.mats = mats;
		}

		
	}
}
